#include "ktloginaccountwgt.h"
#include <QFile>
#include <QKeyEvent>

KtLoginAccountWgt::KtLoginAccountWgt(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::KtLoginAccountWgtClass())
{
	ui->setupUi(this);
	setLoginAccountInfo(m_info);
	initStyle();
	initConnect();
	installEventFilter(this);
	ui->userNameLEdit->installEventFilter(this);
	ui->passwordLEdit->installEventFilter(this);
}

KtLoginAccountWgt::~KtLoginAccountWgt()
{
	delete ui;
}

void KtLoginAccountWgt::setLoginAccountInfo(KtLoginAccountInfo info)
{
	ui->userNameLEdit->setText(info.userName.trimmed());
	ui->passwordLEdit->setText(info.userPassword.trimmed());
	ui->remembCBox->setChecked(info.remember);
	ui->autoLoginCBox->setChecked(info.autoLogin);
	m_info = info;
}

KtLoginAccountInfo KtLoginAccountWgt::getLoginAccountInfo()
{
	KtLoginAccountInfo info;
	info.userName = ui->userNameLEdit->text();
	info.userPassword = ui->userNameLEdit->text();
	info.remember = ui->remembCBox->isChecked();
	info.autoLogin = ui->autoLoginCBox->isChecked();
	return info;
}

void KtLoginAccountWgt::setLoginTitleText(const QString& text)
{
	ui->titleLab->setText(text);
}

void KtLoginAccountWgt::setRemembCBoxVisible(bool bShow)
{
	ui->remembCBox->setVisible(bShow);
}

bool KtLoginAccountWgt::remembCBoxVisible()
{
	return ui->remembCBox->isVisible();
}

void KtLoginAccountWgt::setAutoLoginCBoxVisible(bool bShow)
{
	ui->autoLoginCBox->setVisible(bShow);
}

bool KtLoginAccountWgt::autoLoginCBoxVisible()
{
	return ui->autoLoginCBox->isVisible();
}

void KtLoginAccountWgt::initConnect()
{
	connect(ui->settingBtn, &QPushButton::clicked, this, &KtLoginAccountWgt::settingBtnClickedSgl);
	connect(ui->loginBtn, &QPushButton::clicked, this, &KtLoginAccountWgt::loginBtnClickedSgl);
}

void KtLoginAccountWgt::initStyle()
{
	ui->titleLab->setText(tr("Welcome to\n log in to XXXX client"));
	ui->userNameLEdit->setFocus();
	ui->userNameLEdit->setContextMenuPolicy(Qt::NoContextMenu);
	ui->userNameLEdit->setPlaceholderText(tr("Please enter your username"));
	ui->passwordLEdit->setEchoMode(QLineEdit::EchoMode::Password);
	ui->passwordLEdit->setContextMenuPolicy(Qt::NoContextMenu);
	ui->passwordLEdit->setPlaceholderText(tr("Please enter your password"));
	ui->loginBtn->setObjectName("loginBtn");
	ui->loginBtn->setCursor(Qt::PointingHandCursor);
	ui->loginBtn->setText(tr("Login"));
	ui->settingBtn->setObjectName("settingBtn");
	ui->settingBtn->setCursor(Qt::PointingHandCursor);
	ui->settingBtn->setText(tr("Setting"));
	ui->remembCBox->setText(tr("Remember"));
	ui->remembCBox->setCursor(Qt::PointingHandCursor);
	ui->autoLoginCBox->setText(tr("Automatic login"));
	ui->autoLoginCBox->setCursor(Qt::PointingHandCursor);
	setObjectName("KtLoginAccountWgt");
	QFile file(":/ktloginwidget/resource/qss/ktloginaccountwgt.qss");
	file.open(QFile::ReadOnly);
	setStyleSheet(QLatin1String(file.readAll()));
}

bool KtLoginAccountWgt::eventFilter(QObject* watched, QEvent* event)
{
	switch (event->type())
	{
	case QEvent::KeyPress:
	{
		QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
		switch (keyEvent->key())
		{
		case Qt::Key_Enter:
		case Qt::Key_Return:
		{
			if (watched == ui->userNameLEdit) {
				ui->passwordLEdit->setFocus();
			}
			else if (watched == ui->passwordLEdit) {
				ui->loginBtn->setFocus();
				Q_EMIT ui->loginBtn->clicked(true);
			}
			break;
		}
		default:
			break;
		}
		break;
	}
	default:
		break;
	}
	return false;
}
